package com.panels.cell;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import com.objects.Wagon;

public class ImageValueComboBoxCellRenderer implements TableCellRenderer {
	
	public static void main(String[] args) {
		
		JFrame jf=new JFrame();
		jf.setSize(300,400);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JTable table=new JTable();
		
		DefaultTableModel model=new DefaultTableModel();
		
		model.addColumn("value");
		
		
		for(int i=0;i<3;i++){
			Vector record=new Vector();
			record.add(""+i%2);		
			model.addRow(record);
		}
		table.setModel(model);
		
		
		ImageIcon[] icons = Wagon.getWagonImages();
		
		
		ValuePair vp=new ValuePair("0","val");
		ValuePair vp2=new ValuePair("1","val2");
		ValuePair vp3=new ValuePair("2","val3");

		Vector wagon_types=new Vector();
		wagon_types.add(vp);
		wagon_types.add(vp2);
		wagon_types.add(vp3);
		
		table.getColumnModel().getColumn(0).setCellRenderer(new ValueComboBoxCellRenderer(wagon_types,false));
		table.getColumnModel().getColumn(0).setCellEditor(new ValueComboBoxCellEditor(wagon_types,false));

		
		jf.getContentPane().add(table);
		jf.setVisible(true);
		
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
	
		if(value==null)
			return vcb;

	
		String code=(String) value;
		vcb.setSelectedCode(code);
		return vcb;
		
		
		

	}
	
	ValueComboBox vcb=null;
	
	public ImageValueComboBoxCellRenderer(Vector data,boolean withVoid,ImageIcon[] images) {
		super();
		this.vcb = new ValueComboBox(data,withVoid);
		
		
		ImageComboBoxRenderer renderer = new ImageComboBoxRenderer(images);
		//renderer.setPreferredSize(new Dimension(200, 130));
		this.vcb.setRenderer(renderer);
	}

    
}
